package com.monitor.service.impl;

import com.monitor.dao.ErpReportChildMapper;
import com.monitor.dao.ErpReportMapper;
import com.monitor.entity.ErpReport;
import com.monitor.entity.ErpReportChild;
import com.monitor.service.ErpReportService;
import com.monitor.util.DataUtil;
import com.monitor.util.Result;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 工作报告表 服务实现类
 * </p>
 *
 * @author 
 * @since 2018-11-13
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class ErpReportServiceImpl extends ServiceImpl<ErpReportMapper, ErpReport> implements ErpReportService {

	@Resource
	private ErpReportMapper erpReportMapper;
	
	@Resource
	private ErpReportChildMapper erpReportChildMapper;
	
	@Override
	public Result<Integer> addReport(ErpReport report, List<ErpReportChild> child) {
		// TODO Auto-generated method stub
		try {
			if(report.getId()==null||report.getId()==0) {
				report.setSubmitDate(new Date());
				erpReportMapper.insert(report);
				if(child.size()>0) {
					for(ErpReportChild rc:child) {
						rc.setPid(report.getId());
						erpReportChildMapper.updateAllColumnById(rc);
					}
				}
			}else {
				erpReportMapper.updateAllColumnById(report);
				if(child.size()>0) {
					erpReportChildMapper.delete(new EntityWrapper<ErpReportChild>().eq("pid", report.getId()));
					for(ErpReportChild rc:child) {
						rc.setPid(report.getId());
						erpReportChildMapper.updateAllColumnById(rc);
					}
				}
			}
			return new Result<Integer>(1, 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<>(1, e.getMessage());
		}
		
	}

	@Override
	public Result<Map<String, Object>> selectByReportTime(String time) {
		// TODO Auto-generated method stub
		List<ErpReport> list=erpReportMapper.selectList(new EntityWrapper<ErpReport>().eq("rep_date", time));
		if(list.size()>0) {
			ErpReport report=list.get(0);
			List<ErpReportChild> childList=erpReportChildMapper.selectList(new EntityWrapper<ErpReportChild>().eq("pid", report.getId()));
			Map<String, Object> map=new HashMap<>();
			map.put("report", report);
			map.put("childList", childList);
			return new Result<Map<String,Object>>(map, 1);
		}else {
			return new Result<Map<String,Object>>(null, 1);
		}
		
	}
	
}
